
package com.bei_en.teacher.management.controller;

//import javax.inject.Inject;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bei_en.common.util.OSS.UploadUtil;
import com.bei_en.teacher.management.model.system.vo.SystemResourceVo;
import com.bei_en.teacher.management.model.system.vo.SystemUserVo;
import com.bei_en.teacher.management.other.Consts;
import com.bei_en.teacher.management.service.system.SystemResourceService;
import com.bei_en.teacher.management.service.system.SystemRoleService;
import com.bei_en.teacher.management.service.system.SystemUserService;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;

@Controller
@RequestMapping("/admin")
/**
 * 信息类
 * @author Guocg
 *
 */
public class AdminController extends BaseController {

    Logger log = Logger.getLogger(this.getClass());
    @Autowired
    SystemUserService systemUserService;
    @Autowired
    SystemRoleService systemRoleService;
    @Autowired
    SystemResourceService systemResourceService;
    @Autowired
    private Producer captchaProducer;

    @RequestMapping("/index")
    public String index(Model model, HttpSession session) {

	try {
	    SystemUserVo user = (SystemUserVo) session.getAttribute(Consts.LOGIN_USER_ADMIN);
	    List<SystemResourceVo> resourceList;
	    if (user.getName().equals("admin")) {
		resourceList = systemResourceService.findAll();
	    }else {
		resourceList = systemResourceService.findResourcesByUserId(user.getId());
	    }
//	    List<SysResourceVo> treeList = new TreeUtil().getChildSysResourceVos(list, null);
//	    model.addAttribute("list", treeList);
//	    model.addAttribute("list", treeList);
	    // 登陆的信息回传页面
	    model.addAttribute("resourceList", resourceList);
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return "/admin/index.html";
    }

    @RequestMapping(value = "/captcha")
    public void kaptcha(HttpServletRequest req, HttpServletResponse rsp) {

	ServletOutputStream out = null;
	try {
	    HttpSession session = req.getSession();
	    rsp.setDateHeader("Expires", 0);
	    rsp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	    rsp.addHeader("Cache-Control", "post-check=0, pre-check=0");
	    rsp.setHeader("Pragma", "no-cache");
	    rsp.setContentType("image/jpeg");

	    String capText = captchaProducer.createText();
	    session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);

	    BufferedImage image = captchaProducer.createImage(capText);
	    out = rsp.getOutputStream();
	    ImageIO.write(image, "jpg", out);
	    out.flush();
	} catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    try {
		out.close();
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	}
    }//

    @RequestMapping("/error/404.html")
    public String e404(Model model, HttpSession session) {

	try {
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return "/admin/error/404.html";
    }

    @RequestMapping("/{xx}.jsp1")
    public String general(Model model, @PathVariable String xx, HttpSession session) {

	try {
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return "/admin/"+xx+".jsp";
    }

    @RequestMapping("/{xx}.html")
    public String general1(Model model, @PathVariable String xx, HttpSession session) {

	try {
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return "/admin/"+xx+".html";
    }
    
    @RequestMapping(value = "/uploadPic", method = RequestMethod.POST)
    @ResponseBody
    public Object uploadPic(Model model, MultipartFile file, HttpSession session) {

	Map map = new HashMap();
	try {
	    String picUrl = UploadUtil.upload(file);
	    if (picUrl!=null&&picUrl.equals("sexy")) {
		map.put("statusCode", 300);
		map.put("message", "鉴黄不通过");
		return map;
	    }
	    map.put("statusCode", 200);
	    map.put("filename", picUrl);
	} catch (Exception e) {
	    e.printStackTrace();
	    map.put("statusCode", 300);
	    map.put("message", e.getMessage());
	    return map;
	}
	return map;
    }

    @RequestMapping(value = "/uploadPicRich", method = RequestMethod.POST)
    @ResponseBody
    public Object uploadPicRich(Model model, String dir, MultipartFile imgFile, HttpSession session) {

	Map map = new HashMap();
	if (dir!=null&&!dir.equals("image")) {
	    map.put("error", 1);
	    map.put("message", "非图片");
	    return map;
	}
	try {
	    String picUrl = UploadUtil.uploadUrl(imgFile);
	    System.out.print(picUrl);
	    if (picUrl!=null&&"sexy".equals(picUrl)) {
		map.put("error", 1);
		map.put("message", "鉴黄不通过");
		return map;
	    }

	    map.put("error", 0);
	    map.put("url", picUrl);
	} catch (Exception e) {
	    e.printStackTrace();
	    map.put("error", 1);
	    map.put("message", e.getMessage());
	    return map;
	}
	return map;
    }

    


}